//
//  TTTableHelpItemCell.m
//  ineedish
//
//  Created by Felipe Navarro V. on 01-11-09.
//  Copyright 2009 deis. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "TTTableHelpItemCell.h"
#import "INDefaultStylesheet.h"
#import "TTTableHelpItem.h"
#import "INDefaultStylesheet.h"
///////////////////////////////////////////////////////////////////////////////////////////////////

//static CGFloat kHPadding = 10;
//static CGFloat kVPadding = 5;
static CGFloat kImageWidth = 40;
static CGFloat kImageHeight = 40;
static const CGFloat kSmallMargin = 10;
static const CGFloat kKeyWidth = 75;
static const CGFloat kKeySpacing = 12;


static const CGFloat kDisclosureIndicatorWidth = 23;

static const NSInteger kMessageTextLineCount = 0;
static const NSInteger kSubjectLineCount = 0;
static const CGFloat kMaxLabelHeight = 260;
static const CGFloat kMaxCharsLabel = 180;


@implementation TTTableHelpItemCell

+ (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(id)object {
	TTTableHelpItem* item = object;
//	CGFloat width = tableView.width - kImageWidth - 10;

	
	if (!item.text.font) {
		item.text.font = TTSTYLEVAR(needTextFont);
	}
	
	CGFloat padding = [tableView tableCellMargin]*2 + item.padding.left + item.padding.right;
	if (item.URL) {
		padding += kDisclosureIndicatorWidth;
	}
	
	//CGSize textSize = [item.caption sizeWithFont:TTSTYLEVAR(needSubjectFont)
//							   constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
//								   lineBreakMode:UILineBreakModeWordWrap];
//
//	CGSize other = [item.title sizeWithFont:TTSTYLEVAR(needSubjectFont)
//							   constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
//								   lineBreakMode:UILineBreakModeWordWrap];
//	
	
	item.text.width = tableView.width - kImageWidth -  padding;
	
	return item.text.height + kImageHeight;
	//	return detailTextSize.height + 22 + kVPadding*5;	
	
}




- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {	
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // Initialization code
		self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);

		
		_captionLabel = [[UILabel alloc] init];
		_timestampLabel = [[UILabel alloc] init];
		_imageView2 = [[TTImageView alloc] initWithFrame:CGRectZero];
		[self.contentView addSubview:_timestampLabel];
		[self.contentView addSubview:_captionLabel];
		[self.contentView addSubview:_imageView2];



    }
	
    return self;
}



- (void)layoutSubviews {
	[super layoutSubviews];	
	//	self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
	self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-shadow2.png"]] autorelease];
	self.backgroundColor = TTSTYLEVAR(cellHelpBackgroundColor);
	
	//	self.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-shadow.png"]] autorelease];
	self.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.contentView.height)] autorelease];
	//	self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
	self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
	self.selectedBackgroundView.frame = CGRectMake(0, 0, 320, self.contentView.height);
	
//	self.backgroundColor = [UIColor groupTableViewBackgroundColor];
	//	TTView* view = [[[TTView alloc] initWithFrame:frame] autorelease];
	//	self.view
	
	//Style Right
	//TTStyle* style = [TTShapeStyle styleWithShape:[TTSpeechBubbleShape shapeWithRadius:5 pointLocation:20
//																			 pointAngle:320
//																			  pointSize:CGSizeMake(10,10)] next:
//					   [TTSolidFillStyle styleWithColor:[UIColor whiteColor] next:
//						[TTSolidBorderStyle styleWithColor:[UIColor blackColor] width:1 next:nil]]];
	//Style Left
	
//	TTStyle* style = [TTShapeStyle styleWithShape:[TTSpeechBubbleShape shapeWithRadius:5 pointLocation:200
//																			pointAngle:160
//																			 pointSize:CGSizeMake(20,10)] next:
//					  [TTSolidFillStyle styleWithColor:[UIColor whiteColor] next:
//					   [TTSolidBorderStyle styleWithColor:[UIColor grayColor] width:1 next:nil]]];	
	
	CGFloat left = 4;
//	CGFloat right = 15;
	CGFloat top = 4;
//	CGFloat height = self.contentView.height;
	CGFloat width = self.contentView.width - left;
	
	//Avatar
	//Round Corners to the image
	CALayer * imglayer = [_imageView2 layer];
	[imglayer setMasksToBounds:YES];
	[imglayer setCornerRadius:5.0];
	[imglayer setBorderWidth:0.5];
	[imglayer setBorderColor:[TTSTYLEVAR(needPictureUrlBorderColor) CGColor]];
	
	_imageView2.frame = CGRectMake(8, 8 , kImageWidth, kImageHeight);
	
	//Label DisplayName
	[_captionLabel sizeToFit];
	//	self.captionLabel.top = 10;
	//self.captionLabel.backgroundColor = [UIColor greenColor];
	
	if (_captionLabel.text.length) {
		_captionLabel.frame = CGRectMake(left + kImageWidth + kSmallMargin , top , width, _captionLabel.font.lineHeight);
		_captionLabel.backgroundColor = [UIColor clearColor];				
		//top += _captionLabel.height;
	} else {
		_captionLabel.frame = CGRectZero;
	}	
	
	//Label TimeStamp
	//	self.timestampLabel.backgroundColor = [UIColor blueColor];
	if (_timestampLabel.text.length) {
		_timestampLabel.alpha = !self.showingDeleteConfirmation;
		[_timestampLabel sizeToFit];
		_timestampLabel.left = self.contentView.width  + kSmallMargin - _timestampLabel.width - 28;
		_timestampLabel.top = _captionLabel.top;
//		self.timestampLabel.width -=  self.timestampLabel.width + kSmallMargin*2 + 58;
		_timestampLabel.backgroundColor = TTSTYLEVAR(cellHelpBackgroundColor);
	} else {
		_timestampLabel.frame = CGRectZero;
	}
	self.backgroundView.frame = CGRectMake(0,  self.contentView.height - 21, 320, 20);
	
	//Label Text
	if (self.label.text) {
		self.label.top = _captionLabel.bottom;
		self.label.left = kSmallMargin + kImageWidth;
		self.label.width = self.contentView.width - kImageWidth - kSmallMargin;
	//	self.label.backgroundColor = [UIColor whiteColor];
		self.label.backgroundColor = [UIColor clearColor];
		[self.label sizeToFit];
	} else {
		self.label.frame = CGRectZero;
	}
	
	
	
	
	
//	TTStyle* style = [TTShapeStyle styleWithShape:[TTRoundedLeftArrowShape shapeWithRadius:5] next:
//					  [TTSolidFillStyle styleWithColor:[UIColor whiteColor] next:
//					   [TTSolidBorderStyle styleWithColor:[UIColor grayColor] width:1 next:nil]]];
//	
//	CGRect frame = CGRectMake(kImageWidth, kSmallMargin, 
//							  width - kImageWidth - kSmallMargin, 
//							  height - kSmallMargin);
//	
//	
//	TTView* aview = [[[TTView alloc] initWithFrame:frame] autorelease];
//	//	self.aview.frame = frame;
//	aview.backgroundColor = RGBCOLOR(216, 221, 231);
//	aview.style = style;
//	//	self.contentView.st
//	
//	
//	
//	[self.contentView addSubview:aview];
	
	self.accessoryType = UITableViewCellAccessoryNone;

}


//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//    // Configure the view for the selected state
//}


- (void)dealloc {
//	TT_RELEASE_SAFELY(_cityLabel);
	TT_RELEASE_SAFELY(_captionLabel);
	TT_RELEASE_SAFELY(_timestampLabel);
	TT_RELEASE_SAFELY(_imageView2);
	
	
    [super dealloc];
}



- (id)object {
	return _item;
}



- (void)setObject:(id)object {
	if (_item != object) {
		[super setObject:object];
		
		TTTableHelpItem* item = object;
		
		//DisplayName
		_captionLabel.text = item.caption;
		_captionLabel.textColor = TTSTYLEVAR(needDisplayNameColor);
		_captionLabel.font = TTSTYLEVAR(needDisplayNameFont);
		_captionLabel.textAlignment = UITextAlignmentLeft;
		_captionLabel.contentMode = UIViewContentModeTop;
		_captionLabel.lineBreakMode = UILineBreakModeWordWrap;
		
		//Text
		self.label.textColor = TTSTYLEVAR(needTextColor);
		self.label.font = TTSTYLEVAR(needTextFont);
//		self.label.adjustsFontSizeToFitWidth = YES;
//		self.label.highlightedTextColor = TTSTYLEVAR(highlightedTextColor);
		self.label.textAlignment = UITextAlignmentLeft;
		self.label.contentMode = UIViewContentModeLeft;
//		self.label.lineBreakMode = UILineBreakModeWordWrap;
//		self.label.numberOfLines = kMessageTextLineCount;
		
		//Date
		_timestampLabel.text = [item.timestamp formatShortTime];
		_timestampLabel.font = TTSTYLEVAR(needNDateFont);
		_timestampLabel.textColor = TTSTYLEVAR(needNDateColor);
		_timestampLabel.contentMode = UIViewContentModeLeft;
		
//		self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
//		self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
		
		//		self.selectedBackgroundView.highlighted = true;
		//		self.contentView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
		//		
		_imageView2.URL = item.imageURL;
		
		
    }
}

@end
